/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[TestImage.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package swing.awt;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class TestImage {

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ImageFrame frame = new ImageFrame();
				frame.setVisible(true);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setResizable(false);
			}
		});
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class ImageFrame extends JFrame {

	private static final long serialVersionUID = 3578904098731094710L;

	public ImageFrame() {
		setTitle("Image reading");
		this.setSize(420, 635);
		setLocationByPlatform(true);
		getContentPane().add(new ImagePanel());
	}
}

/**
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 *
 */
class ImagePanel extends JPanel {

	private static final long serialVersionUID = 5323313684369782714L;

	private Image image = null;

	public ImagePanel() {
		try {
			/*
			 * 如果在加载图片时出现 Can't load standard profile: GRAY.pf<br>
			 * 请检查jdk安装路径下的如下目录: jdk1.6.0_05\jre\lib\cmm, 看里面是否缺少文件
			 */
			// 方法1
			image = Toolkit.getDefaultToolkit().getImage(ImageFrame.class.getClassLoader().getResource("__/icon.gif"));
			// 方法2
			image = ImageIO.read(new java.io.File("src/__/icon.gif"));
			// 方法3
			image = ImageIO.read(ImageFrame.class.getClassLoader().getResource("__/icon.gif"));
			// 方法4
			image = new ImageIcon("src/__/icon.gif").getImage();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void paintComponent(Graphics g) {
		if (image == null) {
			return;
		}
		g.drawImage(image, 0, 0, this);
	}
}